package com.jiukuaidao.library.util;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.jiukuaidao.library.glide.CustomImageSizeModel;
import com.jiukuaidao.library.glide.CustomImageSizeModelFutureStudio;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import jp.wasabeef.glide.transformations.CropCircleTransformation;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * Author : PaiTaSuo
 * Date : 2016/5/20
 * Description :
 */
public class ImageGlideUtils {
    private ImageGlideUtils() {
    }

    /**
     * 从资源中加载图片
     *
     * @param activity
     * @param resourceId
     * @param imageView
     */
    public static void IdGlideImages(Activity activity, int resourceId, ImageView imageView) {
        Glide.with(activity).load(resourceId).into(imageView);
    }

    /**
     * 从资源中加载图片
     *
     * @param fragment
     * @param resourceId
     * @param imageView
     */
    public static void IdGlideImages(Fragment fragment, int resourceId, ImageView imageView) {
        Glide.with(fragment).load(resourceId).into(imageView);
    }

    /**
     * 自定义尺寸下请求图片
     *
     * @param activity
     * @param url
     * @param imageView
     * @param resourceId
     */
    public static void CustomSizeGlide(Activity activity, String url, ImageView imageView, int resourceId) {
        CustomImageSizeModel customImageRequest = new CustomImageSizeModelFutureStudio(url);
        Glide.with(activity).load(customImageRequest)
                .placeholder(resourceId) // 设置资源加载过程中的占位Drawable
                .into(imageView);
    }

    /**
     * 自定义尺寸下请求图片
     *
     * @param fragment
     * @param url
     * @param imageView
     * @param resourceId
     */
    public static void CustomSizeGlide(Fragment fragment, String url, ImageView imageView, int resourceId) {
        CustomImageSizeModel customImageRequest = new CustomImageSizeModelFutureStudio(url);
        Glide.with(fragment).load(customImageRequest)
                .placeholder(resourceId) // 设置资源加载过程中的占位Drawable
                .into(imageView);
    }


    /**
     * 加载图片
     *
     * @param activity
     * @param url
     * @param imageView
     * @param resourceId
     */

    public static void GlideImages(Activity activity, String url, ImageView imageView, int resourceId) {
        Glide.with(activity).load(url)
                .placeholder(resourceId) // 设置资源加载过程中的占位Drawable
                .into(imageView);
    }

    /**
     * 加载图片
     *
     * @param fragment
     * @param url
     * @param imageView
     * @param resourceId
     */

    public static void GlideImages(Fragment fragment, String url, ImageView imageView, int resourceId) {
        Glide.with(fragment).load(url)
                .placeholder(resourceId) // 设置资源加载过程中的占位Drawable
                .into(imageView);
    }

    /**
     * ImageView 可能会完全填充，但图像可能不会完整显示。
     *
     * @param activity
     * @param url
     * @param imageView
     * @param resourceId
     * @param width
     * @param hight
     */
    public static void CropGlideImages(Activity activity, String url, ImageView imageView, int resourceId, int width, int hight) {
        Glide.with(activity)
                .load(url)
                .override(width, hight)
                .centerCrop()
                .placeholder(resourceId) // 设置资源加载过程中的占位Drawable
                .into(imageView);
    }

    /**
     * ImageView 可能会完全填充，但图像可能不会完整显示。
     *
     * @param fragment
     * @param url
     * @param imageView
     * @param resourceId
     * @param width
     * @param hight
     */
    public static void CropGlideImages(Fragment fragment, String url, ImageView imageView, int resourceId, int width, int hight) {
        Glide.with(fragment)
                .load(url)
                .override(width, hight)
                .centerCrop()
                .placeholder(resourceId) // 设置资源加载过程中的占位Drawable
                .into(imageView);
    }

    /**
     * 该图像将会完全显示，但可能不会填满整个 ImageView。
     *
     * @param activity
     * @param url
     * @param imageView
     * @param resourceId
     * @param width
     * @param hight
     */
    public static void FitGlideIamges(Activity activity, String url, ImageView imageView, int resourceId, int width, int hight) {
        Glide.with(activity)
                .load(url)
                .override(width, hight)
                .fitCenter()
                .placeholder(resourceId) // 设置资源加载过程中的占位Drawable
                .into(imageView);
    }

    /**
     * 该图像将会完全显示，但可能不会填满整个 ImageView。
     *
     * @param fragment
     * @param url
     * @param imageView
     * @param resourceId
     * @param width
     * @param hight
     */

    public static void FitGlideIamges(Fragment fragment, String url, ImageView imageView, int resourceId, int width, int hight) {
        Glide.with(fragment)
                .load(url)
                .override(width, hight)
                .fitCenter()
                .placeholder(resourceId) // 设置资源加载过程中的占位Drawable
                .into(imageView);
    }


    /**
     * 加载圆形图片
     *
     * @param activity
     * @param url
     * @param imageView
     */
    public static void CircleGlideImages(Activity activity, String url, ImageView imageView, int resourceId) {
        Glide.with(activity)
                .load(url)
                .placeholder(resourceId) // 设置资源加载过程中的占位Drawable
                .bitmapTransform(new CropCircleTransformation(activity)) //圆形裁剪
                .into(imageView);

    }

    public static void CircleGlideImages(Fragment fragment, String url, ImageView imageView, int resourceId) {
        Glide.with(fragment)
                .load(url)
                .placeholder(resourceId) // 设置资源加载过程中的占位Drawable
                .bitmapTransform(new CropCircleTransformation(fragment.getActivity())) //圆形裁剪
                .into(imageView);

    }

    /**
     * 加载圆角图片
     *
     * @param activity
     * @param url
     * @param imageView
     * @param radius
     * @param margin
     */
    public static void CornersGlideImages(Activity activity, String url, ImageView imageView, int radius, int margin) {
        Glide.with(activity).load(url)
                .bitmapTransform(new RoundedCornersTransformation(activity, radius, margin, RoundedCornersTransformation.CornerType.ALL))
                .into(imageView);

    }

    /**
     * 加载圆角图片
     *
     * @param fragment
     * @param url
     * @param imageView
     * @param radius
     * @param margin
     */
    public static void CornersGlideImages(Fragment fragment, String url, ImageView imageView, int radius, int margin) {
        Glide.with(fragment).load(url)
                .bitmapTransform(new RoundedCornersTransformation(fragment.getActivity(), radius, margin, RoundedCornersTransformation.CornerType.ALL))
                .into(imageView);

    }

    /**
     * 清除缓存图片
     */
    public static void ClearCache(final Context context) {
        final Handler handler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == 1) {
                    Glide.get(context).clearMemory();
                }
            }
        };
        ExecutorService es = Executors.newSingleThreadExecutor();
        es.execute(new Runnable() {
            @Override
            public void run() {
                Glide.get(context).clearDiskCache();
                handler.sendEmptyMessage(1);
            }
        });
    }
}
